﻿using BonjourQuotation.DataAccess.Entity_Framework;
using BonjourQuotation.ObjectModel.AccountData;
using BonjourQuotation.ObjectModel.BusinessData;
using BonjourQuotation.ObjectModel.ErrorData;
using BonjourQuotation.ObjectModel.LoggerData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BonjourQuotation.DataAccess.Repositories
{
    public class BaseRepository : IBaseRepository
    {
        private readonly BonDataEntities _context = new BonDataEntities();
        private readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public List<string> GetUserRegions(string UserName)
        {
            List<string> UserRegions = new List<string>();
            List<int> UserRegionsID = new List<int>();
            var EmployeeID = _context.Users.Where(user => user.UserName == UserName).SingleOrDefault().EmployeeID;
            UserRegionsID = _context.UsersQuotaRegions.Where(qreg => qreg.EmployeeID == EmployeeID).Select(qreg => qreg.QuotaRegionID).ToList();
            if (UserRegionsID != null)
                UserRegions = _context.tblQuotaRegions.Where(qreg => UserRegionsID.Contains(qreg.RegionID)).Select(qreg => qreg.Region).ToList();
            return UserRegions == null ? new List<string>() : UserRegions;
        }
        public UserAllPermissionsBData GetPermssions(string UserName)
        {
            UserAllPermissionsBData Data = new UserAllPermissionsBData();
            var User = (from user in _context.Users where user.UserName == UserName select user).SingleOrDefault();
            if (User != null)
            {
                Data.UserRole = User.Role;
                Data.Tools = (from tool in _context.tblTools
                              select new AllTools()
                              {
                                  ToolID = tool.ToolID,
                                  ToolName = tool.ToolName,
                                  ToolTabs = (from tooltab in _context.tblToolTabs
                                              where tooltab.ToolID == tool.ToolID
                                              select new AllToolTabs()
                                              {
                                                  ToolTabID = tooltab.ToolTabID,
                                                  ToolTabName = tooltab.ToolTabName,
                                                  PermFixCol = (from fixcolumn in _context.tblToolFixedColumnCounts
                                                                where fixcolumn.ToolTabID == tooltab.ToolTabID
                                                                && fixcolumn.UserID == User.UserID
                                                                select fixcolumn.FixedColumnCount).FirstOrDefault(),
                                                  ToolTabColumns = (from column in _context.tblToolTabColumns
                                                                    where column.ToolTabID == tooltab.ToolTabID
                                                                    select new AllToolTabColumns()
                                                                    {
                                                                        ToolTabColumnID = column.ToolTabColumnID,
                                                                        ToolTabColumnName = column.ToolTabColumnName,
                                                                        Permisionss = (from perm in _context.tblToolTabPermissions
                                                                                       where perm.ToolTabColumnID == column.ToolTabColumnID
                                                                                       && perm.UserID == User.UserID
                                                                                       select new AllPermissionRowBData
                                                                                       {
                                                                                           PerColumnEdit = perm.CanEdit,
                                                                                           PerColumnView = perm.CanView,
                                                                                           PerHasFilter = perm.HasFilter,
                                                                                           PerColumnLineNo = perm.LineNo,
                                                                                           PerColumnName = column.ToolTabColumnName,
                                                                                       }).FirstOrDefault()
                                                                    }).ToList()

                                              }).ToList()
                              }).ToList();
            }
            else
            {
                Data.Tools = new List<AllTools>();
            }
            return Data;
        }
        public void Dispose()
        {
            _context.Dispose();
        }
    }
}
